package com.bacon.gmall.product.service.impl;


import com.bacon.gmall.common.config.RabbitConst;
import com.bacon.gmall.common.utils.RabbitUtil;
import com.bacon.gmall.model.product.*;
import com.bacon.gmall.product.mapper.*;
import com.bacon.gmall.product.service.BaseManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author： bacon、
 * @version： 1.0
 * @create： 2020-12-19 13:09
 * @description:
 */
@Service
public class BaseManageServiceImpl implements BaseManageService {

    @Autowired
    private RabbitUtil rabbitUtil;

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    @Autowired
    private SpuInfoMapper spuInfoMapper;

    @Autowired
    private BaseSaleAttrMapper baseSaleAttrMapper;


    @Autowired
    private SpuImageMapper spuImageMapper;

    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;

    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrValueMapper;

    @Autowired
    private SkuInfoMapper skuInfoMapper;

    @Autowired
    private SkuImageMapper skuImageMapper;

    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;


    @Autowired
    private SkuSaleAttrValueMapper skuSaleAttrValueMapper;



    @Override
    public List<BaseCategory1> getCategory1() {
        return baseCategory1Mapper.selectList(null);
    }

    @Override
    public List<BaseCategory2> getCategory2(Long category1) {
        return baseCategory2Mapper.selectList(
                new QueryWrapper<BaseCategory2>().eq("category1_id", category1));
    }

    @Override
    public List<BaseCategory3> getCategory3(Long category2) {
        return baseCategory3Mapper.selectList(
                new QueryWrapper<BaseCategory3>().eq("category2_id", category2));
    }

    @Override
    public List<BaseAttrInfo> attrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        return baseAttrInfoMapper.attrInfoList(category1Id, category2Id, category3Id);
    }

    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {

        List<BaseAttrValue> valueList = null;

        //根据有无id选择更新还是添加
        if (baseAttrInfo != null && baseAttrInfo.getId() != null) {

            //1、根据id更新BaseAttrInfo
            baseAttrInfoMapper.updateById(baseAttrInfo);
            valueList = baseAttrInfo.getAttrValueList();
            //2、删除所有id为当前BaseAttrInfo的BaseAttrValue
            baseAttrValueMapper.delete(
                    new QueryWrapper<BaseAttrValue>().eq("attr_id",baseAttrInfo.getId()));

        } else {
            //1、平台属性表
            baseAttrInfoMapper.insert(baseAttrInfo);

            //2、平台属性值表
            valueList = baseAttrInfo.getAttrValueList();

        }

        //添加元素
        if (valueList != null){
            valueList.forEach((attrValue) -> {
                attrValue.setAttrId(baseAttrInfo.getId());
                baseAttrValueMapper.insert(attrValue);
            });
        }
        /*方式二：迭代器
        for (BaseAttrValue baseAttrValue : attrValueList) {
                baseAttrValue.setAttrId(baseAttrInfo.getId());
                baseAttrValueMapper.insert(baseAttrValue);
            }*/
    }

    @Override
    public IPage<BaseTrademark> baseTrademark(Long page, Long limit) {

        return baseTrademarkMapper.selectPage(new Page<BaseTrademark>(page, limit), null);

    }

    @Override
    public BaseTrademark getBaseTrademark(Long id) {
        return baseTrademarkMapper.selectById(id);
    }

    @Override
    public List<BaseAttrValue> getAttrValueList(Long attrId) {

        //1、先查询3集分类的属性名称
        BaseAttrInfo attrInfo = baseAttrInfoMapper.selectById(attrId);
        Long id = attrInfo.getId();

        //2、 3集分类的属性名称所有种类
        List<BaseAttrValue> attrValueList = baseAttrValueMapper.selectList(
                new QueryWrapper<BaseAttrValue>().eq("attr_id", id));
        return attrValueList;
    }

    @Override
    public void updateBaseTrademark(BaseTrademark baseTrademark) {
        baseTrademarkMapper.updateById(baseTrademark);
    }


    @Override
    public IPage<SpuInfo> spuList(Integer page, Long limit, Long category3Id) {
        return spuInfoMapper.selectPage(
                new Page<>(page, limit),
                new QueryWrapper<SpuInfo>().eq("category3_id", category3Id));
    }

    @Override
    public List<BaseTrademark> getTrademarkList() {
        return baseTrademarkMapper.selectList(null);
    }

    @Override
    public List<BaseSaleAttr> getSaleAttrList() {
        return baseSaleAttrMapper.selectList(null);
    }


    @Override
    public void saveBaseTrademark(BaseTrademark baseTrademark) {
        baseTrademarkMapper.insert(baseTrademark);
    }

    @Override
    public void removeBaseTrademark(Long trademarkId) {
        baseTrademarkMapper.deleteById(trademarkId);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void saveSpuInfo(SpuInfo spuInfo) {
        //保存spuinfo,并返回id
        spuInfoMapper.insert(spuInfo);

        List<SpuImage> imageList = spuInfo.getSpuImageList();
        Long infoId = spuInfo.getId();
        imageList.forEach(image ->{
            image.setSpuId(infoId);
            spuImageMapper.insert(image);
        });
        List<SpuSaleAttr> saleAttrList = spuInfo.getSpuSaleAttrList();
        saleAttrList.forEach(saleAttr ->{
            //1、插入数据
            saleAttr.setSpuId(infoId);
            spuSaleAttrMapper.insert(saleAttr);

            //2、另外一张表数据插入
            String saleAttrName = saleAttr.getSaleAttrName();
            List<SpuSaleAttrValue> saleAttrValueList = saleAttr.getSpuSaleAttrValueList();
            saleAttrValueList.forEach(saleAttrValue->{
                saleAttrValue.setSpuId(infoId);
                saleAttrValue.setSaleAttrName(saleAttrName);
                spuSaleAttrValueMapper.insert(saleAttrValue);
            });
        });
    }

    @Override
    public List<SpuImage> spuImageList(Long spuId) {
        return spuImageMapper.selectList(
                new QueryWrapper<SpuImage>().eq("spu_id",spuId));
    }

    @Override
    public void onSale(Long skuId) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(SkuInfo.ISSALE);
        skuInfoMapper.updateById(skuInfo);
        //使用rabbitMQ 通知其他微服务
        rabbitUtil.sendMessage(RabbitConst.EXCHANGE_DIRECT_GOODS,
                RabbitConst.ROUTING_GOODS_UPPER,skuId);
    }

    @Override
    public void cancelSale(Long skuId) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(SkuInfo.CANCELSALE);
        skuInfoMapper.updateById(skuInfo);
        //使用rabbitMQ 通知其他微服务
        rabbitUtil.sendMessage(RabbitConst.EXCHANGE_DIRECT_GOODS,
                RabbitConst.ROUTING_GOODS_LOWER,skuId);
    }

    @Override
    public List<SpuSaleAttr> spuSaleAttrList(Long spuId) {
        return spuSaleAttrMapper.spuSaleAttrList(spuId);
    }

    @Override
    public void saveSkuInfo(SkuInfo skuInfo) {
        skuInfoMapper.insert(skuInfo);
        Long skuInfoId = skuInfo.getId();
        List<SkuImage> skuImageList = skuInfo.getSkuImageList();
        List<SkuAttrValue> skuAttrValueList = skuInfo.getSkuAttrValueList();
        List<SkuSaleAttrValue> skuSaleAttrValueList = skuInfo.getSkuSaleAttrValueList();

        skuImageList.forEach(skuImage -> {
            skuImage.setSkuId(skuInfoId);
            skuImageMapper.insert(skuImage);
        });

        skuAttrValueList.forEach(skuAttrValue -> {
            skuAttrValue.setSkuId(skuInfoId);
            skuAttrValueMapper.insert(skuAttrValue);
        });

        skuSaleAttrValueList.forEach(skuSaleAttrValue -> {
            //库存表外键
            skuSaleAttrValue.setSkuId(skuInfoId);

            //商品表外键
            skuSaleAttrValue.setSpuId(skuInfo.getSpuId());
            skuSaleAttrValueMapper.insert(skuSaleAttrValue);
        });
    }

    @Override
    public IPage<SkuInfo> list(Long page, Long limit) {
        return skuInfoMapper.selectPage(new Page<>(page,limit),null);
    }
}
